package com.situ.study.servlet;

import com.situ.study.pojo.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

// http://localhost:8080/JavaWeb/index.jsp
// http://localhost:8080/JavaWeb/user
@WebServlet("/user")
public class UserServlet extends HttpServlet {

    //访问Servlet时候默认访问service方法
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("UserServlet.service");

        List<User> list = new ArrayList<>();
        list.add(new User(1, "zhansgan1", 23, "男"));
        list.add(new User(2, "zhansgan2", 23, "男"));
        list.add(new User(3, "zhansgan3", 23, "男"));
        list.add(new User(4, "zhansgan4", 23, "男"));
        for (User user : list) {
            System.out.println(user);
        }

        //先把数据放到req中
        req.setAttribute("list", list);
        //转发到user_list.jsp页面进行展示
        req.getRequestDispatcher("/user_list.jsp").forward(req, resp);
    }
}
